-- Trading View Menu

if not menu then

if not script_name then script_name = string.gsub(string.gsub(debug.getinfo(1).short_src, "%.[^%.]*$", ""), "^.*[\\/]", "") end


dofile (getScriptPath() .. "\\..\\lib\\lib.lua")


menu = {}


dofile (ScriptsRootPath .. "processor\\processor.lua")
dofile (ScriptsRootPath .. "processor\\log.lua")

dofile (getScriptPath() .. "\\window.lua")
dofile (getScriptPath() .. "\\portfolio.lua")
dofile (getScriptPath() .. "\\chart_tv.lua")
dofile (getScriptPath() .. "\\label_tv.lua")
dofile (getScriptPath() .. "\\risk.lua")
dofile (getScriptPath() .. "\\profit.lua")
dofile (getScriptPath() .. "\\action.lua")


orders.do_save = true
assets_delay = 0
start_delay = 0


processor:AddHandler("OnRun", function(t)
	assets_delay = false
end)


processor:AddInit(1, function()
	processor:InitLog(log_file)
	processor:SetTimer(500)
	message("Assets " .. lib:tostring(lib:GetAssets()) .. " Money " .. lib:tostring(lib:GetMoney()))
end)


processor:AddInit(1, function()
	if not lib:isConnected() then return true end
	if not client_code then client_code = lib:GetUser() end
	if not client_code then return true end
	if not assets_delay then assets_delay = processor:GetTime(5000) end
	if not processor:IsDelay(assets_delay) then return true end
	
	if not script_id then script_id = client_code .. "#" .. script_name end
	if not log_file then log_file = lib.path.log .. script_id .. ".log" end
end)

--[ [
processor:AddInit(1, function()
	--if not lib:singleton(script_name .. "." .. client_code) then
	if not lib:singleton(script_name) then
		return true
		--fail("Duplicate script instance")
		--processor:stop(0)
	end
end)
--]]

processor:AddInit(function()
	--orders:CleanTakeLimits()
	processor:log("started")
end)


processor:AddTimer(function()
	for k, v in pairs(menu) do
		if type(v) == "table" then
			if type(v.changed) == "table" then
				v.changed = {}
			else
				v.changed = nil
			end
		end
	end
end)


processor:AddTimer(function()
	if start_delay and processor:IsDelay(start_delay) then
		start_delay = nil
		lib:AddLine(lib.path.events .. "menu.start", lib:GetUser())
	end
end)

--[[
processor:AddTimer(function()
	collectgarbage("collect")
end)
--]]

end	-- of file

